package com.uniknow.app.ui;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

import com.uniknow.app.AppConfig;
import com.uniknow.app.AppContext;
import com.uniknow.app.AppLifeCycle;
import com.uniknow.app.R;
import com.uniknow.app.manager.UserManager;
import com.uniknow.app.operate.BaseListener;

/**
 * 
 * @{# SplashActivity.java Create on 2013-5-2 下午9:10:01
 * 
 *     class desc: 启动画面 (1)判断是否是首次加载应用--采取读取SharedPreferences的方法
 *     (2)是，则进入GuideActivity；否，则进入MainActivity (3)3s后执行(2)操作
 * 
 *     <p>
 *     Copyright: Copyright(c) 2013
 *     </p>
 * @Version 1.0
 * 
 * 
 */
public class SplashActivity extends BaseActivity {
	boolean isFirstIn = false;

	private static final int GO_MAIN = 1000;
	private static final int GO_GUIDE = 1001;
	private static final int GO_LOGIN = 1002;
	// 延迟3秒
	private static final long SPLASH_DELAY_MILLIS = 1000;


	/**
	 * Handler:跳转到不同界面
	 */
	private Handler mHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			Intent intent = null;
			switch (msg.what) {
			case GO_MAIN:
				intent = new Intent(SplashActivity.this, MainActivity.class);
				break;
			case GO_GUIDE:
				intent = new Intent(SplashActivity.this, GuideActivity.class);
				break;
			case GO_LOGIN:
				intent = new Intent(SplashActivity.this, LoginActivity.class);
				break;
			default:
				intent = new Intent(SplashActivity.this, MainActivity.class);
			}
			if(AppConfig.DeBug){
				UserManager.getManager(mContext).init();
				AppContext.user = UserManager.getManager(mContext).userBean;
				AppConfig.UserName = AppContext.user.getUserName();
				AppConfig.U_ID = AppContext.user.getUserid()+"";
				AppConfig.PassWord = AppContext.user.getPassWord();
				UserManager.getManager(mContext).add(UserManager.getManager(mContext).userBean);
				intent = new Intent(SplashActivity.this, MainActivity.class);
			}
			SplashActivity.this.startActivity(intent);
			SplashActivity.this.finish();
			super.handleMessage(msg);
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_splash);

		init();
	}

	private void init() {
		// 读取SharedPreferences中需要的数据
		SharedPreferences preferences = getSharedPreferences(AppConfig.SHAREDPREFERENCES_NAME, MODE_PRIVATE);
		// 取得相应的值，如果没有该值，说明还未写入，用true作为默认值
		isFirstIn = preferences.getBoolean("isFirstIn", true);
		AppConfig.TencentOpenId = preferences.getString("TencentOpenId", "");
		AppConfig.TencentExpiresIn = preferences.getString("TencentExpiresIn", "0");
		AppConfig.TencentAccessToken = preferences.getString("TencentAccessToken", "");

		// 判断程序与第几次运行，如果是第一次运行则跳转到引导界面，否则跳转到主界面
		if (!isFirstIn) {
			// 使用Handler的postDelayed方法，3秒后执行跳转到MainActivity
			if(false){//是否登录
				mHandler.sendEmptyMessageDelayed(GO_MAIN, SPLASH_DELAY_MILLIS);
			}else{
				mHandler.sendEmptyMessageDelayed(GO_LOGIN, SPLASH_DELAY_MILLIS);
			}
			AppLifeCycle.onStart(new BaseListener() {
				
				@Override
				public void onSuccess(Object o) {
					// TODO Auto-generated method stub
					
				}
				
				@Override
				public void onError(String e) {
					// TODO Auto-generated method stub
					
				}
			});
		} else {
			AppLifeCycle.onStartAtFrist(new BaseListener() {
				
				@Override
				public void onSuccess(Object o) {
					// TODO Auto-generated method stub
					
				}
				
				@Override
				public void onError(String e) {
					// TODO Auto-generated method stub
					
				}
			});
			mHandler.sendEmptyMessageDelayed(GO_GUIDE, SPLASH_DELAY_MILLIS);
		}
		//获取屏幕信息
		AppContext.DM = getApplicationContext().getResources().getDisplayMetrics();
		
	}


}
